package com.atguigu.flink.chapter07.window;

import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;

/**
 * @Author lzc
 * @Date 2022/7/7 10:23
 */
public class Flink05_Window_ProcessFunction_1 {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        env
            .socketTextStream("hadoop162", 9999)
            .map(line -> {
                String[] data = line.split(",");
                return new WaterSensor(
                    data[0],
                    Long.valueOf(data[1]),
                    Integer.valueOf(data[2])
                );
            })
            .keyBy(WaterSensor::getId)
            .window(TumblingProcessingTimeWindows.of(Time.seconds(5)))
            .aggregate(
                new AggregateFunction<WaterSensor, Avg, Double>() {
                    // 初始化一个累加器
                    @Override
                    public Avg createAccumulator() {
                        // 这个窗口内第一个元素来的时候执行
                        System.out.println("Flink05_Window_ProcessFunction_1.createAccumulator");
                        return new Avg();
                    }
                    
                    // 累加
                    @Override
                    public Avg add(WaterSensor value, Avg acc) {
                        // 每来一个元素执行一次
                        System.out.println("Flink05_Window_ProcessFunction_1.add");
                        acc.sum += value.getVc();
                        acc.count++;
                        return acc;
                    }
                    
                    // 获取最终的聚合结果
                    @Override
                    public Double getResult(Avg acc) {
                        // 窗口关闭的时候执行
                        System.out.println("Flink05_Window_ProcessFunction_1.getResult");
                        return acc.sum * 1.0 / acc.count;
                    }
                    
                    // 合并累加器: 只有当是session窗口的时候才有效
                    @Override
                    public Avg merge(Avg a, Avg b) {
                        System.out.println("Flink05_Window_ProcessFunction_1.merge");
                        return null;
                    }
                }
            
            )
            .print();
        
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private static class Avg {
        public Integer sum = 0;
        public Long count = 0L;
    }
}
/*
窗口处理函数:
    增量
        简单增量
            sum max min
            maxBy minBy
        
        reduce
            当输入类型和聚合的类型一致的key选择reduce
        
        
        aggregate
        
    
    
    全量
        process


 */